package cn.zucc.edu.handyzucc.service.impl;

import cn.zucc.edu.handyzucc.dao.CommentDao;
import cn.zucc.edu.handyzucc.dao.PostDao;
import cn.zucc.edu.handyzucc.entity.Follow;
import cn.zucc.edu.handyzucc.entity.Post;
import cn.zucc.edu.handyzucc.entity.PostFavorite;
import cn.zucc.edu.handyzucc.entity.PostVote;
import cn.zucc.edu.handyzucc.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostDao postDao;
    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Post> getAllPosts() {
        return postDao.getAllPosts();
    }

    @Override
    public List<Post> getPostsByAuthor(Integer post_author) {
        return postDao.getPostsByAuthor(post_author);
    }

    @Override
    public List<Post> getPostsByFollow(Integer follow_follower) {
        return postDao.getPostsByFollow(follow_follower);
    }

    @Override
    public List<Post> getFavoritePosts(Integer postfavorite_userid) {
        return postDao.getFavoritePosts(postfavorite_userid);
    }

    @Override
    public List<Post> getPostsByKeyword(String keyword) {
        return postDao.getPostsByKeyword(keyword);
    }

    @Override
    public Integer addPost(Post post) {
        if (postDao.addPost(post) == 1)
            return post.getPost_id();
        else return 0;
    }

    @Override
    public boolean deletePost(Integer post_id) {
        return postDao.deletePost(post_id) == 1;
    }

    @Override
    public boolean deletePostInAdmin(Integer post_id) {
        if (postDao.deletePostInAdmin(post_id) == 1 && commentDao.deletePostAllComment(post_id) == 1){
            System.out.println("\nfavorite delete succeed:"+postDao.deleteFavoritePostInAdmin(post_id));
            System.out.println("\nvote delete succeed:"+postDao.deletePostVoteInAdmin(post_id));
            return true;
        }
        else return false;
    }

    @Override
    public boolean deleteFavoritePost(Integer postfavorite_postid, Integer postfavorite_userid) {
        return postDao.deleteFavoritePost(postfavorite_postid, postfavorite_userid) == 1;
    }

    @Override
    public Post getPostDetail(Integer post_id) {
        return postDao.getPostDetail(post_id);
    }

    @Override
    public boolean ifAlreadyFollow(Integer follow_leader, Integer follow_follower) {
        Follow follow = postDao.ifAlreadyFollow(follow_leader, follow_follower);
        if (follow != null && follow.getFollow_id() > 0)
            return true;
        else return false;
    }

    @Override
    public boolean addFollow(Integer follow_leader, Integer follow_follower) {
//        int effectedNum = postDao.addFollow(new Follow(follow_leader, follow_follower));
//        if (effectedNum == 1)
//            return true;
//        else return false;
        return postDao.addFollow(new Follow(follow_leader, follow_follower)) == 1;
    }

    @Override
    public boolean cancelFollow(Integer follow_leader, Integer follow_follower) {
//        int effectedNum = postDao.cancelFollow(follow_leader, follow_follower);
//        if (effectedNum == 1)
//            return true;
//        else return false;
        return postDao.cancelFollow(follow_leader, follow_follower) == 1;
    }

    @Override
    public boolean getPostVote(Integer post_id, Integer user_id) {
//        PostVote postVote = postDao.getPostVote(post_id, user_id);
//        if (postVote != null)
//            return true;
//        else return false;
        return postDao.getPostVote(post_id, user_id) != null;
    }

    @Override
    public boolean getPostFavorite(Integer post_id, Integer user_id) {
        PostFavorite postFavorite = postDao.getPostFavorite(post_id, user_id);
        if (postFavorite != null)
            return true;
        else return false;
    }

    @Override
    public boolean addPostVote(Integer post_id, Integer user_id) {
        int effectedNum = postDao.addPostVote(new PostVote(post_id, user_id));
        if (effectedNum == 1)
            return true;
        else return false;
    }

    @Override
    public boolean cancelPostVote(Integer post_id, Integer user_id) {
        int effectedNum = postDao.cancelPostVote(post_id, user_id);
        if (effectedNum == 1)
            return true;
        else return false;
    }

    @Override
    public boolean addPostFavorite(Integer post_id, Integer user_id) {
        int effectedNum = postDao.addPostFavorite(new PostFavorite(post_id, user_id));
        if (effectedNum == 1)
            return true;
        else return false;
    }

    @Override
    public boolean cancelPostFavorite(Integer post_id, Integer user_id) {
        int effectedNum = postDao.cancelPostFavorite(post_id, user_id);
        if (effectedNum == 1)
            return true;
        else return false;
    }

    @Override
    public boolean updatePost(Integer post_id, String type, Integer step) {
        int effectedNum = 0;
        switch (type) {
            case "vote":
                effectedNum = postDao.updatePostVotes(post_id, step);
                break;
            case "comment":
                effectedNum = postDao.updatePostComments(post_id, step);
                break;
            case "favorite":
                effectedNum = postDao.updatePostFavorites(post_id, step);
                break;
            default:
                break;
        }
        return effectedNum == 1;
    }
}
